import VueCookies from 'vue-cookies';
import axios from 'axios';
import { Toast,Dialog,Notify } from 'vant';



const fdclInit = {
    
    install(Vue) {



        Vue.use(VueCookies);

        Vue.$cookies.config('30d')

        Vue.use(Dialog);
        Vue.use(Notify);




        if(window.location.hostname.indexOf('127.0.0.1')>=0){


        Vue.prototype.$axios = axios.create({

            baseURL : 'http://127.0.0.1:8000' 

        })
        }
        else{

        Vue.prototype.$axios = axios.create({})

        }


        // request interceptor
        Vue.prototype.$axios.interceptors.request.use(
        config => {
            // do something before request is sent
            /*
            if (store.getters.token) {
            // let each request carry token
            // ['X-Token'] is a custom headers key
            // please modify it according to the actual situation
            //config.headers['X-Token'] = getToken()
            }*/
            //config.headers.post["X-CSRFToken"] = Vue.$cookies.get("csrftoken")
            return config
        },
        error => {
            // do something with request error
            console.log(error) // for debug
            return Promise.reject(error)
        }
        )

        // response interceptor
        Vue.prototype.$axios.interceptors.response.use(
        /**
         * If you want to get http information such as headers or status
         * Please return  response => response
         */

        /**
         * Determine the request status by custom code
         * Here is just an example
         * You can also judge the status by HTTP Status Code
         */
        response => {

            const res = response.data

            // if the custom code is not 20000, it is judged as an error.
            if (res.code !== 20000) {

            Toast({
                message: res.message || 'Error',
                type: 'fail',
                duration: 5 * 1000
            })

            // 50008: Illegal token; 50012: Other clients logged in; 50014: Token expired;
            if (res.code === 50008 || res.code === 50012 || res.code === 50014) {
                // to re-login
                Dialog.confirm({
                title:'提示',
                message: '后端的API返回时，显示您没有登录!',
                confirmButtonText: '登录',
                cancelButtonText: '取消',
                type: 'warning'
                }).then(() => {
                window.location.href = '/login/?next=' + encodeURIComponent(window.location.href)
                })
            }
            return Promise.reject(new Error(res.message || 'Error'))
            } else {
            return res
            }
        },
        error => {
            console.log('err' + error) // for debug
            Toast({
            message: error.message,
            type: 'fail',
            duration: 5 * 1000
            })
            return Promise.reject(error)
        }
        )


        
    },
};

export {fdclInit}